import service from '../utils/request';


//岗位条件查询
const getJobsApi = async (queryParams) => {
  try {
    const response = await service.post('/api/job/jobs',queryParams);
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}
//岗位状态修改
const setStatusApi = async (jobId,statusId) => {
  try {
    const response = await service.put('/api/job/status',{"jobId": jobId,"statusId": statusId});
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}
//查询公司类型
const getCompanyTypeApi = async () => {
  try {
    const response = await service.get('/api/job/companyType');
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}
//查询校招招聘类型
const getSchoolJobTypeApi = async () => {
  try {
    const response = await service.get('/api/job/school/jobType');
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}
//查询实习招聘类型
const getInternJobTypeApi = async () => {
  try {
    const response = await service.get('/api/job/intern/jobType');
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}

//查询所有招聘类型
const getJobTypeApi = async () => {
  try {
    const response = await service.get('/api/job/jobType');
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}

//查询招聘招聘对象
const getCandidateTypeApi = async () => {
  try {
    const response = await service.get('/api/job/candidate');
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}
//查询学历
const getEducationalBackgroundTypeApi = async () => {
  try {
    const response = await service.get('/api/job/educationalBackground');
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}
//查询投递状态类型
const getStatusTypeApi = async () => {
  try {
    const response = await service.get('/api/job/status');
    return response;
  } catch (error) {
    console.error('Error adding student:', error);
    throw error;
  }
}

// 批量添加岗位
const batchAddJobsApi = async (jobList) => {
  try {
    const response = await service.post('/api/admin/job', jobList);
    return response;
  } catch (error) {
    console.error('Error batch adding jobs:', error);
    throw error;
  }
};

// 根据 ID 获取岗位详情（用于修改）
const getJobByIdApi = async (id) => {
  try {
    const response = await service.get(`/api/admin/job/${id}`);
    return response;
  } catch (error) {
    console.error('Error fetching job by id:', error);
    throw error;
  }
};

// 更新岗位信息
const updateJobApi = async (job) => {
  try {
    const response = await service.put('/api/admin/job', job);
    return response;
  } catch (error) {
    console.error('Error updating job:', error);
    throw error;
  }
};

// 批量删除岗位
const batchDeleteJobsApi = async (ids) => {
  try {
    const response = await service.delete(`/api/admin/job`,{
      params: { ids }
    });
    return response;
  } catch (error) {
    console.error('Error batch deleting jobs:', error);
    throw error;
  }
};

// 下载模板
const downloadTemplateApi = async () => {
  try {
    const res = await service.get('/api/admin/job/download/template', {
      responseType: 'blob',   // 关键：返回二进制
    })
    return res;
  }
    catch (error) {
      console.error('下载失败：', error);
      throw error;
    }
}

//上传excel
const excelApi = async (formData) => {
  try {
    const res = await service.post('/api/admin/job/excel', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    return res;
  }
    catch (error) {
      console.error('上传失败：', error);
      throw error;
    }
}


export { 
  getJobsApi,
  setStatusApi,
  getCompanyTypeApi,
  getSchoolJobTypeApi,
  getInternJobTypeApi,
  getJobTypeApi,
  getCandidateTypeApi,
  getEducationalBackgroundTypeApi,
  getStatusTypeApi,

  batchAddJobsApi,
  getJobByIdApi,
  updateJobApi,
  batchDeleteJobsApi,
  downloadTemplateApi,
  excelApi
};

